import jpa.Entity.Course;
import jpa.Entity.Student;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * The sevelet class to insert Person into database
 */
@SuppressWarnings("Duplicates")
@WebServlet(name = "CreateServlet", urlPatterns = {"/Create"}, loadOnStartup = 2)
public class CreateServlet extends HttpServlet {

    private static final EntityManagerFactory entityManagerFactory;
    private static final String PERSISTENCE_UNIT_NAME = "MyJPADemoPU";

    static {
        entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    }

    private EntityManager getEntityManager() {
        return entityManagerFactory.createEntityManager();
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request  servlet request
     * @param response servlet response
     */
    private void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        EntityManager em = getEntityManager();

        EntityTransaction tx = em.getTransaction();

        try {
            String sID = request.getParameter("sID");
            String sName = request.getParameter("sName");
            String sex = request.getParameter("sex");
            String age = request.getParameter("age");
            String cID = request.getParameter("cID");
            String cName = request.getParameter("cName");

            Student student = new Student(sID,sName,sex,age);
            Course course1 = new Course(cID,cName);
         //   Course course2 = new Course(2,"javaee");

            student.getCourse().add(course1);
           //student.getCourse().add(course2);

            //begin a transaction
            tx.begin();
            //create an em.
            //Since the em is created inside a transaction, it is associsated with
            //the transaction
            em.persist(student);
            em.persist(course1);
           // em.persist(course2);
            //commit transaction which will trigger the em to
            //commit newly created entity into database
            tx.commit();

            //Forward to ListPerson servlet to list persons along with the newly
            //created person above
            // request.getRequestDispatcher("ListPerson").forward(request, response);
        } catch (Exception ex) {
            throw new ServletException(ex);
        } finally {
            //close the em to release any resources held up by the persistebce provider
            em.close();
        }

        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().println("添加记录成功");
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request  servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request  servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>

}
